package com.kaibes.admin.permission;

import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.kaibes.module.permission.PermissionCheckerImpl;
import com.kaibes.module.permission.db.entity.Power;
import com.kaibes.web.api.ApiLinkData;

@Component
public class PowerUtils {

    @Autowired
    private PermissionCheckerImpl permissionChecker;
    
    public boolean contains(List<Power> powers, ApiLinkData apiLinkData) {
        for (Power power : powers) {
            if (Objects.equals(power.getName(), apiLinkData.getName())) {
                return true;
            }
        }
        return false;
    }
    
    public Integer getId(List<Power> powers, ApiLinkData apiLinkData) {
        for (Power power : powers) {
            if (Objects.equals(power.getName(), apiLinkData.getName())) {
                return power.getId();
            }
        }
        return null;
    }
    
    public Power createPower(ApiLinkData apiLinkData, Integer roleId) {
        Power power = new Power();
        power.setRoleId(roleId);
        power.setName(apiLinkData.getName());
//        power.setPattern(apiLinkData.getPatternList().toString());
//        power.setMethod(apiLinkData.getMethod());
        return power;
    }
    
    public boolean hasPermission(Integer userId, String uri) {
        return permissionChecker.hasPermission(userId, uri, "GET");
    }
    
    public boolean hasPermission(Integer userId, String uri, String method) {
        return permissionChecker.hasPermission(userId, uri, method);
    }
    
}
